<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\User;
use App\Admin\Repositories\UserAddress;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use App\Models\User as UserModel;

class UserController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User(), function (Grid $grid) {
            $grid->id->sortable();
            $grid->name->hide();
            $grid->avatar->image('', 40, 40);
            $grid->mobile;
            $grid->nickname;
            $grid->email->hide();
            $grid->sex->using(UserModel::$sexMaps);
            $grid->province;
            $grid->city;
            $grid->country;
            $grid->language->hide();
            $grid->provider->using(UserModel::$providerMaps)->filter(
                Grid\Column\Filter\In::make(UserModel::$providerMaps)
            );
            $grid->created_at;
            $grid->updated_at->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new User(), function (Show $show) {
            $show->id;
            $show->name;
            $show->nickname;
            $show->mobile;
            $show->email;
            $show->openid;
            $show->access_token;
            $show->sex->using(\App\Models\User::$sexMaps);
            $show->province;
            $show->city;
            $show->country;
            $show->language;
            $show->avatar->image();
            $show->provider;
            $show->unionid;
            $show->created_at;
            $show->updated_at;
            $show->addresses(function ($model) {
                $grid = new Grid(new UserAddress());
                $grid->model()->where('user_id', $model->id);

                $grid->resource('/admin/user-addresses');

                $grid->id();
                $grid->full_address('详细地址');
                $grid->contact_name('联系人');
                $grid->contact_phone('电话号码');

                $grid->filter(function ($filter) {
                    $filter->like('full_address')->width('300px');
                });

                return $grid;
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->text('nickname');
            $form->mobile('mobile');
            $form->email('email');
            $form->text('openid');
            $form->text('access_token');
            $form->text('sex');
            $form->text('province');
            $form->text('city');
            $form->text('country');
            $form->text('language');
            $form->text('avatar');
            $form->text('provider');
            $form->text('unionid');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
